package com.sjb.cp.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.sjb.cp.common.BaseResponse;
import com.sjb.cp.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author:sjb
 * @CreateTime:2024-12-09
 * @Description: AOP切面全局对业务异常以及RuntimeException的捕获
 * @Version：1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> businessExceptionHandler(BusinessException businessException){
        log.error("BusinessException", businessException);
        return ResultUtils.error(businessException.getCode(), businessException.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public BaseResponse<?> runTimeExceptionHandler(RuntimeException e){
        log.error("RuntimeException", e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"系统异常");
    }

    @ExceptionHandler(NotLoginException.class)
    public BaseResponse<?> notLoginException(NotLoginException exception){
        log.error("NotLoginException", exception);
        return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR,exception.getMessage());
    }

    @ExceptionHandler(NotPermissionException.class)
    public BaseResponse<?> notPermissionException(NotPermissionException exception){
        log.error("NotPermissionException", exception);
        return ResultUtils.error(ErrorCode.NO_AUTH_ERROR,exception.getMessage());
    }

}
